<?php

namespace app\api\controller\yqfff;

use think\cache\driver\Redis;

/**
 * @author zero
 * @Time 2018/12/21
 *
 * Class Fffachieve
 * @package app\api\controller\flyflyfly
 *
 * 一起飞飞飞成就相关接口
 */
class Fffachieve extends BaseFff
{
    // 获取成就信息
    public function achieve_info()
    {
        $data['list'] = [
            '1_' . $this->acc->game_num,       // 累计已玩游戏次数
            '2_' . $this->acc->resurrect_num,  // 累计复活次数
            '3_' . $this->acc->block_num,      // 累计打掉砖块数
            '4_' . $this->acc->share_num,      // 累计分享次数
            '5_' . $this->acc->login_day_num,  // 登录天数
            '6_' . $this->acc->max_score,      // 当局最高分
            '7_' . $this->acc->all_score,      // 累计得分
            '8_' . $this->acc->live_second,    // 单局存活时间
            '9_' . $this->acc->share_user_num, // 邀请用户数
        ];
        if ($this->acc->achieved) {
            $data['achieved'] = explode(',', $this->acc->achieved);
        } else {
            $data['achieved'] = [];
        }
        $this->result('ok', $data);
    }

    // 完成成就并获取奖励
    public function get_achieve_award()
    {
        $achieved_id = $this->request->post('achieved_id') ?: -1; // 成就id
        $is_double = $this->request->post('is_double') ?: 1; // 资源类奖励倍数
        // 对比该成绩是否已完成
        $achieved = explode(',', $this->acc->achieved);
        if (!in_array($achieved_id, $achieved)) {
            // 获取成就条件
            $achieves = $this->get_json_file('fff_achievement_define', 'flyflyfly/achievement_define');
            $achieve = $achieves->$achieved_id;
            if ($achieve && $achieve->condition) {
                // 对比条件完成度
                if ($this->handle_condition($achieve->condition)) {
                    // 遍历获取奖励
                    $awards = explode(',', $achieve->award);
                    foreach ($awards as $award) {
                        $this->handle_award($award, $is_double);
                    }
                    // 新增已完成成就id
                    if ($this->acc->achieved) {
                        $this->acc->achieved = $this->acc->achieved . ',' . $achieved_id;
                    } else {
                        $this->acc->achieved = $achieved_id;
                    }
                    // 更新保存数据,并返回奖励字符串
                    $this->acc->save();
                    $data['coin'] = $this->acc->coin;
                    $data['diamond'] = $this->acc->diamond;
                    $data['award'] = $achieve->award;
                    $this->result('ok', $data);
                }
            }
        }
        $this->result('fail');
    }

    // 获取签到数据
    public function sign_in_info()
    {
        $data['sign_in_round'] = floor($this->acc->sign_in_num / 7) == 0 ? 1 : 2; // 签到轮数
        $data['is_sign_in'] = $this->acc->is_sign_in; // 今日是否签过到
        if ($this->acc->is_sign_in == 1) {
            $data['sign_in_num'] = floor($this->acc->sign_in_num % 7); // 已签到的天数
        } else {
            $data['sign_in_num'] = floor($this->acc->sign_in_num % 7) + 1; // 下一次签到天数
        }
        $this->result('sign in', $data);
    }

    // 签到领取奖励
    public function sign_in()
    {
        $double = $this->request->post('double') ?: 1;
        $sign_condition = $this->get_json_file('fff_sign_condition', 'flyflyfly/sign');
        $sign_round = floor($this->acc->sign_in_num / 7) + 1;
        $sign_in_num = floor($this->acc->sign_in_num % 7) + 1;

        $sign = $sign_condition->$sign_in_num;
        // 查询轮数
        if ($sign_round == 1) {
            $award = $sign->award1;
        } else {
            $award = $sign->award2;
        }
        $this->handle_award($award, $double);
        $this->acc->sign_in_num += 1;
        $this->acc->is_sign_in = 1;
        if ($this->acc->save() !== false) {
            $data['coin'] = $this->acc->coin;
            $data['diamond'] = $this->acc->diamond;
            $this->result('ok', $data);
        }

        $this->result('sign fail');
    }

    // 单独增加资源(金币,钻石,复活币),每天有次数上限
    public function add_resource()
    {
        $type = $this->request->post('type');
        switch ($type) {
            case 'coin':
                $this->acc->coin += $this->add_coin_award;
                $this->handle_add_resource_num($this->get_coin_resource_num, $this->acc->openid . '_fff_get_coin_num');
                break;
            case 'diamond':
                $this->acc->diamond += $this->add_diamond_award;
                $this->handle_add_resource_num($this->get_diamond_resource_num, $this->acc->openid . '_fff_get_diamond_num');
                break;
        }
        if ($this->acc->save() !== false) {
            $data['coin'] = $this->acc->coin;
            $data['diamond'] = $this->acc->diamond;
            $data = array_merge($data, $this->get_add_resource_num());
            $this->result('ok', $data);
        }
        $this->result('fail');
    }

    // 利用redis处理获取资源次数上限
    public function handle_add_resource_num($condition_num, $key)
    {
        $redis = new Redis();
        // 今晚12点
        $end_day_time = strtotime(date('Y-m-d', strtotime('+1 day')));

        $num = $redis->get($key);
        if ($num) {
            if ($num < $condition_num) {
                $num += 1;
                $redis->set($key, $num, $end_day_time - time());
            } else {
                $this->result('fail');
            }
        } else {
            $redis->set($key, 1, $end_day_time - time());
        }
    }

    // 幸运转盘数据
    public function lucky_wheel()
    {
        $game_const = $this->get_json_file('fff_game_const', 'flyflyfly/GameConstDefine');
        $award_list = explode(',', $game_const->lucky_wheel->award);
        // 拿5个普通奖励
        $index_list = array_rand($award_list, 5);
        $list = array();
        for ($i = 0; $i < 5; $i++) {
            $list[$i + 1] = $award_list[$index_list[$i]];
        }
        // 拿取一个该用户没有得皮肤奖励
        $plane_skin_list = $this->get_json_file('fff_plane_skin_list_define', 'flyflyfly/plane_skin');
        $have_skin_list = explode(',', $this->acc->plane_skin_list);
        $not_have_skin_list = array();
        // 遍历取出玩家未拥有的
        foreach ($plane_skin_list as $skin) {
            if ($skin && $skin->bullet == -1 && !in_array($skin->id, $have_skin_list)) {
                array_push($not_have_skin_list, $skin->id);
            }
        }
        if (empty($not_have_skin_list)) {
            $list[6] = $award_list[array_rand($award_list, 1)];
        } else {
            $list[6] = '4_' . $not_have_skin_list[array_rand($not_have_skin_list, 1)];
        }
        // 返回数据
        $resp['num'] = $this->acc->lucky_wheel_num;
        $resp['is_video'] = $this->acc->lucky_wheel_video;
        $resp['list'] = $list;
        $this->result('ok', $resp);
    }

    // 获取幸运转盘奖励
    public function handle_lucky_wheel_award()
    {
        $award = $this->request->post('award');
        $multiple = $this->request->post('multiple') ?: 1;
        if ($award) {
            // 处理奖励
            $this->handle_award($award, $multiple);
            $this->acc->lucky_wheel_num -= 1;
            if ($this->acc->save() !== false) {
                $data['coin'] = $this->acc->coin;
                $data['diamond'] = $this->acc->diamond;
                $this->result('ok', $data);
            }
        }
        $this->result('fail');
    }

    // 看视频获取抽奖次数
    public function add_lucky_wheel_num()
    {
        if ($this->acc->lucky_wheel_video == 0) {
            $this->acc->lucky_wheel_num = 5;
            $this->acc->lucky_wheel_video = 1;
            $this->acc->save();
            $this->result('ok');
        }
        $this->result('fail');
    }

    // 更新用户奖励
    public function handle_award($award, $multiple)
    {
        $arr = explode('_', $award);
        $type = intval($arr[0]);
        $num = intval($arr[1]);
        // 判断奖励类型
        switch ($type) {
            case '1': // 金币
                $this->acc->coin += $num * $multiple;
                break;
            case '2': // 钻石
                $this->acc->diamond += $num * $multiple;
                break;
            case '3': // 子弹皮肤
                if (in_array($num, explode(',', $this->acc->bullet_skin_list))) {
                    return;
                } else {
                    $this->acc->bullet_skin_list = $this->acc->bullet_skin_list . ',' . $num;
                }
                break;
            case '4': // 飞机皮肤
                if (in_array($num, explode(',', $this->acc->plane_skin_list))) {
                    return;
                } else {
                    $this->acc->plane_skin_list = $this->acc->plane_skin_list . ',' . $num;
                }
                break;
        }
    }

}
